package com.billionsfinance.hessian.core;

import com.billionsfinance.hessian.db.HessianServerDao;
import com.billionsfinance.hessian.db.core.pool.DBPoolMgr;
import com.billionsfinance.hessian.db.impl.HessianServerDaoImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @fileName: HessianServerInitFactory.java
 * @author: WeiHui.Zhang
 * @date: 2016-08-04  14:16
 * @version: v1.0.0
 */
public class HessianServerInitFactory {

	private static final Logger LOGGER = LoggerFactory.getLogger(HessianClientInitFactory.class);

	private String driverClass;
	private String jdbcUrl;
	private String user;
	private String password;

	public void setDriverClass(String driverClass) {
		this.driverClass = driverClass;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void init() {
		LOGGER.info("Init hessian Server config....");
		if (StringUtils.isAnyBlank(driverClass, jdbcUrl, user, password)) {
			throw new RuntimeException("Please init driverClass,jdbcUrl, user,password etc.");
		}
		try {
			DBPoolMgr.initPool(driverClass, jdbcUrl, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		LOGGER.info("Loading hessian data config....");
		HessianServerDao serverDao = new HessianServerDaoImpl();
		List<ServerInfo> serverInfoList = serverDao.findAll();
		for (ServerInfo serverInfo : serverInfoList) {
			LOGGER.info("Loading hessian data : path==>{},interface==>{},class==>{}", serverInfo.getAccessPath(), serverInfo.getInterfaceName(), serverInfo.getServiceClassName());
			HessianCacheManager.setServerInfo(serverInfo.getAccessPath(), serverInfo);
		}
		LOGGER.info("Loading hessian Server success!");
	}

}
